﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Meeting.IDal;

namespace XF.Meeting.DAL
{
    public class UpLoadDal : BaseDAL, IUpLoadDal
    {
        //public Task<Tuple<bool, string,string>> UpLoad(string fileUrl,string fileName)
        //{
        //    bool bResult = true;

        //    string strError = "";

        //    string ServerPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;

        //    IOExtension.MakeDir(true, $@"{ServerPath}\UpLoad\MeetingRoomImg\");

        //    string Upload = $@"{ServerPath}\UpLoad\MeetingRoomImg\";
            
        //    if (!Directory.Exists(Upload))
        //    {
        //        Directory.CreateDirectory(Upload);
        //    }
        //    string FromFilePath = Path.Combine(fileUrl, fileName);

        //    FileStream FromStream = new FileStream(FromFilePath, FileMode.Open, FileAccess.Read);

        //    byte[] byteArray = new byte[FromStream.Length];

        //    if (FromStream.Length > 512000)
        //    {

        //        FromStream.Close();
        //        strError = "上传文件不能大于500kb";
        //        bResult = false;
        //        var tupp = new Tuple<bool, string, string>(bResult, strError, "");
        //        return Task.FromResult(tupp);

        //    }
        //    FromStream.Read(byteArray, 0, byteArray.Length);

        //    FromStream.Close();

        //    string ToFilePath = Path.Combine(Upload, fileName);

        //    FileStream ToStream = new FileStream(ToFilePath, FileMode.Create, FileAccess.Write);

        //    ToStream.Write(byteArray, 0, byteArray.Length);

        //    ToStream.Close();

        //    var tup = new Tuple<bool, string,string>(bResult, strError, "图片地址Url");

        //    return Task.FromResult(tup);
        //}

        //public Task<(bool, string, string)> FileUpload(Stream File)
        //{
            
        //    string strError = "";
        //    bool bResult = true;
        //    var currentDate = DateTime.Now;
        //    string ServerPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
        //    IOExtension.MakeDir(true, $@"{ServerPath}\UpLoad\MeetingRoomImg\");

        //    string Upload = $@"{ServerPath}\UpLoad\MeetingRoomImg\";

        //    try
        //    {
        //        var filePath = $"/UploadFile/{currentDate:yyyyMMdd}/";

        //        //创建每日存储文件夹
        //        if (!Directory.Exists(Upload + filePath))
        //        {
        //            Directory.CreateDirectory(Upload + filePath);
        //        }

        //        if (File != null)
        //        {
        //            //文件后缀
        //            //var fileExtension = Path.GetExtension(File.g);//获取文件格式，拓展名

        //            //判断文件大小
        //            var fileSize = File.Length;

        //            if (fileSize > 1024 * 500) //10M TODO:(1mb=1024X1024b)
        //            {

        //                strError = "上传文件不能大于500kb";
        //                bResult = false;
                       
        //                return Task.FromResult((bResult, strError, ""));
        //            }

        //            //保存的文件名称(以名称和保存时间命名)
        //            //var saveName = File["file"].FileName.Substring(0, File.Name.LastIndexOf('.')) + "_" + currentDate.ToString("HHmmss") + fileExtension;

        //            //文件保存
        //            using (var fs = System.IO.File.Create(Upload + filePath))
        //            {
        //                File.CopyTo(fs);
        //                fs.Flush();
        //            }

        //            //完整的文件路径
        //            var completeFilePath = Path.Combine(filePath);

                   
        //            return Task.FromResult((bResult, strError, completeFilePath));
        //        }
        //        else
        //        {
                    
        //            return Task.FromResult((bResult, strError, "未检测到文件信息"));
        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        bResult = false;
        //        strError = "上传异常：" + ex.ToString();
               
        //        return Task.FromResult((bResult, strError, ""));
        //    }

        //}
    }

}
